<?php

/**
 * 会员控制器
 */
namespace Custom\Controller;
use Think\Controller;
class BannarController extends UserController {


    function __construct()
    {
        parent::__construct();
        $this->M_bannar = M('Bannar');
        $this->s_id = session('s_id');
    }


    public function index(){
        if($this->s_id){
            $list = $this->M_bannar->where('s_id = '.$this->s_id)->select();
            $this->assign('list',$list);
        }
        $this->display();
    }

    /**
     *  首页图修改
     */
    public function edit(){
      if(!$this->s_id){
          die($this->error('请先选择门店！',U('goods/index')));
      }else{
          $id = I('get.id',0,'intval');
          if($id){
              $info = $this->M_bannar->find($id);
              $this->assign('info',$info);
          }

          if(IS_POST){
              $this->M_bannar->sort = I('post.sort');
              $this->M_bannar->s_id = $this->s_id;
              $this->M_bannar->bannar_name = I('post.bannar_name');
              $upload = new \Think\Upload();
              $upload->maxSize   =     3145728 ;// 设置附件上传大小，默认3M
              $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
              $upload->rootPath  =     './'; // 设置附件上传根目录
              $upload->savePath  =     './Uploads/data/bannar_img/'; // 设置附件上传（子）目录// 上传文件
              $img_info   =   $upload->upload();
              if(!$img_info) {// 上传错误提示错误信息
                  //$this->error($upload->getError());
              }else{// 上传成功
                  $this->M_bannar->bannar_img = $img_info['bannar_img']['savepath'].$img_info['bannar_img']['savename'];
              }
              //echo $this->M_bannar->bannar_img;exit;
              if($id){
                  if($this->M_bannar->save())$this->success('添加成功！',__MODULE__.'/bannar');
                  else $this->error('系统错误，请于管理员联系！');
              }else{
                  if($this->M_bannar->add())$this->success('添加成功！',__MODULE__.'/bannar');
                  else $this->error('系统错误，请于管理员联系！');
              }
          }else{
              $this->display();
          }
      }
    }

    /**
     *  状态修改
     */
    public function is_show(){
        $id = $_GET['id'];
        $info = $this->M_bannar->find($id);
        $is_show = $info['is_show']?0:1;
        $this->M_bannar->is_show = $is_show;
        $this->M_bannar->save();
        $is_show?$this->ajaxReturn(array('result'=>'yes')):$this->ajaxReturn(array('result'=>'no'));
    }


    public function delete(){
        $id = I('get.id');
        if(!$id)
            $this->ajaxReturn(array('result'=>'break'));
        $this->M_bannar->where('bannar_id = '.$id)->delete()?$this->ajaxReturn(array('result'=>'tp')):$this->ajaxReturn(array('result'=>'break'));
    }




}